package civilian;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.proto.AchieveREInitiator;

public class SendFinishDownload {

	AID sender;

	/**
	 * Send by the Civilians to Evacuators to inform they exit the building
	 */
	public void addBehaviour(Agent agent, AID sender) {
		ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
		msg.setProtocol(agents.UtilsAgents.PROTOCOL_DOWNLOAD);
		msg.setContent(agents.UtilsAgents.OUT);
		msg.setSender(agent.getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setType(agents.UtilsAgents.EVACUATOR_AGENT);
		this.sender = sender;
		msg.addReceiver(sender);
		agent.addBehaviour(new SendFinishWork(agent, msg));
	}

	public class SendFinishWork extends AchieveREInitiator {
		/**
         *
         */
		private static final long serialVersionUID = 1L;

		public SendFinishWork(Agent arg0, ACLMessage arg1) {
			super(arg0, arg1);
		}

		/**
		 * Receive inform from the manager harvester, and the content is
		 * "Be_scout".
		 */
		@Override
		protected void handleInform(ACLMessage msg) {
			try {
				//
				if ((msg.getProtocol()
						.equals(agents.UtilsAgents.PROTOCOL_DOWNLOAD))
						&& (msg.getContent().equals(agents.UtilsAgents.OK))) {
					System.out.println("Evacuator informed that im safe."
							+ msg.getSender());
				} else
					System.out
							.println("Error in protocol in send download, repeat it.");
				// Cal tornar a cridar el protocol load.
			}

			catch (Exception e) {
				System.out.println("exception in handle inform" + e.toString());
			}

		}
	}
}